package com.xhy.tool.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhy.tool.annotation.Authority;
import com.xhy.tool.authority.XhyAuthority;
import com.xhy.tool.entity.Role;
import com.xhy.tool.entity.User;
import com.xhy.tool.entity.UserRole;
import com.xhy.tool.service.RoleService;
import com.xhy.tool.service.UserRoleService;
import com.xhy.tool.service.UserService;
import com.xhy.tool.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 * 设计方案1：
 * http://localhost:8080/ authorize/permission/initMenu
 *  http://localhost:8080/ authorize/permission/get/1xxd1223213
 *
 * 设计方案2:
 *  使用自定义授权注解，以及aop的环绕通知
 *
 *
 * @author B站-xhyOVO
 * @since 2022-06-04
 * 该类用于演示作用
 */
@RestController
@RequestMapping("/authorize/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleService roleService;




    /**
     * 用户列表
     * @param current
     * @param limit
     * @param name
     * @return
     */
    @GetMapping("/list")
//    @Authority(value = "user:list")
    public R list(@RequestParam(value = "page",defaultValue = "1") Long current,
                  @RequestParam(value = "limit",defaultValue = "15") Long limit,
                  @RequestParam(required = false) String name
                  ){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(name)){
            wrapper.like("name",name);
        }

        Page<User> page = userService.page(new Page<>(current, limit), wrapper);

        page.getRecords().stream()
                .forEach(user -> {
                    // 根据用户id获取角色id
                    List<Integer> rIds = userRoleService.list(new QueryWrapper<UserRole>().eq("u_id", user.getId()).select("r_id")).stream().map(UserRole::getRId).collect(Collectors.toList());
                    if (!ObjectUtils.isEmpty(rIds)){
                        // 根据角色表查出对应的角色名字
                        List<String> roleNames = roleService.listByIds(rIds).stream().map(Role::getName).collect(Collectors.toList());
                        user.setRoleName(roleNames);
                    }
                });

        return R.ok().data("data",page.getRecords()).count(page.getTotal());
    }

    @PostMapping("/add")
    @Authority("user:add")
    public R add(HttpServletRequest request){

        return R.ok();
    }

    @PutMapping("/update")
    @Authority("user:update")
    public R update(){

        return R.ok();
    }
    @DeleteMapping("/delete")
    @Authority("user:delete")
    public R delete(){

        return R.ok();
    }

}

